interface Module {
  count: number;
  message: string;
  // 异步方法
  asyncMethod<T, U>(input: Promise<T>): Promise<Action<U>>;
  // 同步方法
  syncMethod<T, U>(action: Action<T>): Action<U>;
}
interface Action<T> {
  payload?: T;
  type: string;
}

type Transform<X> = X extends (
  input: Promise<infer T>
) => Promise<Action<infer U>>
  ? (input: T) => Action<U> // promise方法改成asyncMethod<T, U>(input: T): Action<U>;
  : X extends (action: Action<infer T>) => Action<infer U> // promise方法改成asyncMethod<T, U>(input: T): Action<U>;
  ? (action: T) => Action<U>
  : never;

type Connect<T> = {
  [K in keyof T as T[K] extends (...args: any[]) => any ? K : never]: Transform<
    T[K]
  >;
};

// 这个要求的结果
type Result = {
  asyncMethod<T, U>(input: T): Action<U>;
  syncMethod<T, U>(action: T): Action<U>;
};

/* type Connect<T extends object> = {
  [K in keyof T as T[K] extends (...args: any[]) => any
    ? K
    : never]: T[K] extends <T, U>(input: Promise<T>) => Promise<Action<U>>
    ? { <T, U>(input: T): Action<U> }
    : T[K] extends <T, U>(action: Action<T>) => Action<U>
    ? <T, U>(action: T) => Action<U>
    : T[K];
}; */
//实现类型Connect，要求 Connect<odule>的结果为上面的 Result
//如果函数是异步函数，要求自动解析出来Promise中的类型:

class MyModule {
  count = 0;
  message = "Hello";
  asyncMethod(input: Promise<number>) {
    return input.then((i) => {
      return {
        payload: i,
        type: "asyncMethod",
      };
    });
  }
  syncMethod(action: Action<number>) {
    return {
      payload: action.payload,
      type: "syncMethod",
    };
  }
}
type F = Connect<MyModule>;
